/**
 *  @file SDLMenuSection.cpp
 *
 *  @date 23-nov-2009
 *  @author Ives van der Flaas
 */

#include <iostream>
#include <cassert>
#include <tr1/memory>
#include "SDLMenuSection.h"
#include "SDLButton.h"
#include "SDLAudioManager.h"
#include "SDLApplication.h"
#include "SDLZoomDrawer.h"
#include "SDLRectangleRepresentation.h"
#include "vector2.h"


namespace si_sdl
{

using si::Vector2;
const si::Vector2 buttonSize(200,50);

SDLMenuSection::SDLMenuSection(
		SDLApplication& parent,
		SDLWindow& win):
	SDLApplicationSection(parent, win)
{


	SDLButton b(
		fPlayGameButton,
		si::Vector2(
				SDLZoomDrawer::get()->requestDrawableRegion().getX()/2-buttonSize.getX()/2,
				150),
		buttonSize,
		"Play Game",
		*this,
		&SDLApplicationSection::processButtonClick);

	SDLButton c(
			fOptionsButton,
			si::Vector2(
					SDLZoomDrawer::get()->requestDrawableRegion().getX()/2-buttonSize.getX()/2,
					220),
			buttonSize,
			"Options",
			*this,
			&SDLApplicationSection::processButtonClick);

	SDLButton d(
				fHighscoresButton,
				si::Vector2(
						SDLZoomDrawer::get()->requestDrawableRegion().getX()/2-buttonSize.getX()/2,
						290),
				buttonSize,
				"Highscores",
				*this,
				&SDLApplicationSection::processButtonClick);

	SDLButton e(
					fQuitButton,
					si::Vector2(
							SDLZoomDrawer::get()->requestDrawableRegion().getX()/2-buttonSize.getX()/2,
							360),
					buttonSize,
					"Quit Game",
					*this,
					&SDLApplicationSection::processButtonClick);


	SDLButton f(
				fMusicOn,
				si::Vector2(
					SDLZoomDrawer::get()->requestDrawableRegion().getX()/2-buttonSize.getX()/2,
					150),
					buttonSize,
					"Music On",
					*this,
					&SDLApplicationSection::processButtonClick);

	SDLButton g(
				fMusicOff,
				si::Vector2(
					SDLZoomDrawer::get()->requestDrawableRegion().getX()/2-buttonSize.getX()/2,
					220),
					buttonSize,
					"Music Off",
					*this,
					&SDLApplicationSection::processButtonClick);

	SDLButton h(
				fBack,
				si::Vector2(
					SDLZoomDrawer::get()->requestDrawableRegion().getX()/2-buttonSize.getX()/2,
					290),
					buttonSize,
					"Back",
					*this,
					&SDLApplicationSection::processButtonClick);



	// Now hide music on & off buttons because they're only visible in options mode
	f.hide();
	g.hide();
	h.hide();


	addButton(b);
	addButton(c);
	addButton(d);
	addButton(e);
	addButton(f);
	addButton(g);
	addButton(h);

	alignButtons();

	fBanner = std::tr1::shared_ptr<SDLImageRepresentation>(
									new SDLImageRepresentation("images/banner.png"));



}

SDLMenuSection::~SDLMenuSection()
{
	// TODO Auto-generated destructor stub
}

void SDLMenuSection::youreStarting()
{
	alignButtons();
}
void SDLMenuSection::newFrame(double time)
{
	// draw background
	SDLRectangleRepresentation::drawRect(si::Vector2(0,0), SDLZoomDrawer::get()->requestDrawableRegion(), Color(0,0,0));

	for(unsigned int i = 0; i < fButtons.size(); i++)
	{
		fButtons[i]->draw();
	}

	// draw banner
	fBanner->draw(si::Vector2(-4.0,3), si::Vector2(8,2));

}

void SDLMenuSection::addButton(SDLButton& but)
{
	std::tr1::shared_ptr<SDLButton> thisButton(new SDLButton(but));
	fButtons.push_back(thisButton);
}

void SDLMenuSection::onSDLEvent(SDL_Event &event)
{
	SDLApplicationSection::onSDLEvent(event);

	// pass events on through to buttons...
	for(std::vector<std::tr1::shared_ptr<SDLButton> >::iterator it = fButtons.begin();
			it != fButtons.end();
			it++)
	{
		(*it).get()->onSDLEvent(event);
	}

	if(event.type == SDL_VIDEORESIZE)
	{
		alignButtons();

	}
}

void SDLMenuSection::alignButtons()
{
	// center all buttons horizontally
	for(std::vector<std::tr1::shared_ptr<SDLButton> >::iterator it = fButtons.begin();
			it != fButtons.end();
			it++)
	{
		(*it)->setHorizontalPosition(
						SDLZoomDrawer::get()->requestDrawableRegion().getX()/2-buttonSize.getX()/2);
	}

	const double height = SDLZoomDrawer::get()->requestDrawableRegion().getY();

	// align buttons vertically
	fButtons[fPlayGameButton]->setVerticalPosition(height-300);
	fButtons[fOptionsButton]->setVerticalPosition(height-230);
	fButtons[fHighscoresButton]->setVerticalPosition(height-160);
	fButtons[fQuitButton]->setVerticalPosition(height-90);

	fButtons[fMusicOn]->setVerticalPosition(height-300);
	fButtons[fMusicOff]->setVerticalPosition(height-230);
	fButtons[fBack]->setVerticalPosition(height-160);
}
void  SDLMenuSection::processButtonClick(ButtonID sender)
{
	if(sender == fPlayGameButton)
		fParent.changeSectionTo(si::GAME);

	if(sender == fHighscoresButton)
		fParent.changeSectionTo(si::HIGHSCORES);

	if(sender == fQuitButton)
			fWantsExit = true;

	if(sender == fOptionsButton)
	{
		// hide all buttons
		fButtons[fPlayGameButton]->hide();
		fButtons[fOptionsButton]->hide();
		fButtons[fHighscoresButton]->hide();
		fButtons[fQuitButton]->hide();

		fButtons[fMusicOn]->show();
		fButtons[fMusicOff]->show();
		fButtons[fBack]->show();
	}

	if(sender == fBack)
	{
		// Hide options buttons & show normal buttons
		fButtons[fMusicOn]->hide();
		fButtons[fMusicOff]->hide();
		fButtons[fBack]->hide();

		fButtons[fPlayGameButton]->show();
		fButtons[fOptionsButton]->show();
		fButtons[fHighscoresButton]->show();
		fButtons[fQuitButton]->show();
	}

	if(sender == fMusicOn)
	{
		SDLAudioManager::get().beLoud();
	}

	if(sender == fMusicOff)
	{
		SDLAudioManager::get().beSilent();
	}


}

}
